package org.komarichin.struts2.iterceptors;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.komarichin.Constants;

import javax.servlet.http.HttpServletRequest;

/**
 * User: Mykhaylo Komarichyn
 * Date: 12.01.2010
 * Time: 19:20:20
 */
public class SavePathIntercepter extends AbstractInterceptor {
    private static final long serialVersionUID = 6862194470559036598L;

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        HttpServletRequest req = ServletActionContext.getRequest();
        String fullReqPath = req.getRequestURL().toString();
        String reqPath = req.getRequestURI();

        String query = StringUtils.defaultIfEmpty(req.getQueryString(), StringUtils.EMPTY);
        if (StringUtils.isNotEmpty(query)) {
            query = "?" + query;
        }

        ActionContext.getContext().getSession().put(Constants.FULL_REQ_PATH, fullReqPath + query);
        ActionContext.getContext().getSession().put(Constants.REQ_PATH, reqPath + query);
        return invocation.invoke();
    }
}
